/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bpmlab.shift.dao;

import bpmlab.shift.dao.service.GenericDAO;
import bpmlab.shift.dao.service.ItemDAO;
import bpmlab.shift.entity.Cardapio;
import bpmlab.shift.entity.Item;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

/**
 *
 * @author Mikael Lima
 */
public class ItemDAOJpa implements ItemDAO {

    private final GenericDAO<Item> dao = DAOFactory.criarGenericDAO(Item.class);

    @Override
    public void salvar(Item o) throws DAOException {
        dao.salvar(o);
    }

    @Override
    public void excluir(Item o) throws DAOException {
        dao.excluir(o);
    }

    @Override
    public Item obter(Object id) {
        return dao.obter(id);
    }

    @Override
    public List<Item> obterTodos() {
        return dao.obterTodos();
    }

    @Override
    public List<Item> obterItensPorCardapio(Cardapio cardapio) throws DAOException {
        EntityManager em = JpaUtil.getEntityManager();
        String jpql = "select i from Item i where :cardapio member of i.cardapios";
        TypedQuery<Item> query = em.createQuery(jpql, Item.class)
                .setParameter("cardapio", cardapio);
        List<Item> resultado = query.getResultList();
        return resultado;
    }

}
